package com.example.tests;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.DataProvider;

import com.example.fw.ApplicationManager;

public class TestBase {
	protected  static ApplicationManager app;
	
	@BeforeSuite
	public void setUp() throws Exception {
		app = new ApplicationManager ();		
	}

	@AfterSuite
	public void tearDown() throws Exception {
		app.stop ();
	}
	
	@DataProvider
	public Iterator<Object[]> randomValidGroupGenerator (){
		List<Object[]>	list = new ArrayList<Object[]>();
		for (int i = 0; i < 5; i++){
			GroupData group = new GroupData()
			.withName(generateRandomString())
			.withHeader(generateRandomString())
			.withFooter(generateRandomString());
		list.add(new Object[]{group});
		}
		return list.iterator();
	}
	
	public String generateRandomString() {
		if (rnd.nextInt(3) == 0){
			return "";
		} else {
			return "test" + rnd.nextInt();
		}
	}
	
	static String[] months = {"-", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
	Random rnd = new Random();
	
	@DataProvider
	public Iterator<Object[]> randomValidContactGenerator (){
		List<Object[]>	list = new ArrayList<Object[]>();
		for (int i = 0; i < 5; i++){
			ContactData contact = new ContactData()
			.withFirstname(generateRandomString())
			.withLastname(generateRandomString())
			.withAddress(generateRandomString())
			.withHomephone(generateRandomString())
			.withMobilephone(generateRandomString())
			.withWorkphone(generateRandomString())
			.withEmail(generateRandomString())
			.withEmail2(generateRandomString())
			.withDate(generateRandomDay())
			.withMonth(generateRandomMonth())
			.withYear(generateRandomYear())
			.withAddress2(generateRandomString())
			.withPhone2(generateRandomString());
			
			list.add(new Object[]{contact});
		}
		return list.iterator();
	}

	private String generateRandomDay() {
		int day = rnd.nextInt(32);
		if (day == 0) {
			return "-";
		}
		return Integer.toString(day);
	}
	
	private String generateRandomMonth() {
		int month = rnd.nextInt(months.length);
		return months[month];
	}
	
	private String generateRandomYear() {
		int year = rnd.nextInt(10000);
		return Integer.toString(year);
	}
}